
#include "comm.hpp"

// 服务端管理管道文件

int main() {
  // 创建管道信道
  PipeInit myfifo;

  // 打开管道文件
  int fd = open(PIPEFILE, O_RDONLY);
  if (fd < 0) {
    cerr << "SERVER OPEN PIPE FAIL" << endl;
    exit(FIFO_OPEN_ERR);
  }
  cout << "Server open file done" << endl;
  // 开始进行通信
  char buffer[SIZE] = {0};  // 清空 当做字符串读取
  while (true) {
    int x = read(fd, buffer, sizeof(buffer));
    if (x > 0) {
      buffer[x] = '\0';
      cout << "server get a massage from client# ";
      cout << buffer << endl;
    }
    if (x == 0) {
      cout << "Client quit" << endl;
      break;
    }
  }

  // 结束通信关闭管道
  close(fd);
  return 0;
}